<template>
	<view class="mb-4 pd-1">
		<!-- 显示车辆单选列表 -->
		<radio-group view class="car-list" @change="carbindChange">
			<label class="car-list-item" v-for="(item,index) in vehicleList">
				<radio :value="item"></radio>
				<p>车牌号 {{item.plateNo}}</p>
				<p>引擎编号 {{item.engineNo}}</p>
				<p>车辆类型 : {{item.type}}</p>
			</label>
		</radio-group>
		<!-- 显示时间选择器 -->
		<p>选择时间</p>
		<view class="date-pick">
			<picker mode="date" @change="dateChange">
				<view>{{date}}</view>
			</picker>
			<picker mode="time" @change="timeChange">
				<view>{{time}}</view>
			</picker>
		</view>
		<!-- 显示检车地点 -->
		<view class="check-place" @click="checkPlaceShow = !checkPlaceShow">
			<p>检车地点</p>
			<p>{{checkPlace.placeName}}</p>
			<p>{{checkPlace.remarks}}</p>
			<p>{{checkPlace.address}}</p>
			<p>{{checkPlace.phone}}</p>
		</view>
		<!-- 检测地点选择器 -->
		<picker-view class="picker-view" v-show="checkPlaceShow" :value="checkPlace" @change="checkPlaceChange">
			<picker-view-column>
				<view class="item" v-for="(item,index) in checkPlaceList" :key="index">{{item.placeName}}</view>
			</picker-view-column>
		</picker-view>
		<button @click="setAppointment">预约</button>
		<!-- 底部导航栏组件 -->
		<BottomTabbar :items="tabbarItems"></BottomTabbar>
	</view>
</template>

<script>
	import {
		http
	} from "../../utils/request.js"
	import {
		items
	} from "./tabBatItems.js"
	http()
	export default {
		data() {
			return {
				tabbarItems: items,
				vehicleList: [],
				checkPlaceList: [],
				bindCar: {},
				date: "2023-1-11", 
				time: "17:30",
				checkPlace : "",
				checkPlaceShow : false
			}
		},
		onBackPress() {
			uni.switchTab({
				url: "../index/index"
			})
			return true
		},
		created() {
			// 获取已绑定的车辆
			http({
				url: "/prod-api/api/traffic/car/list"
			}).then((res) => {
				// 当没有内容时直接跳转到绑定界面
				if (res.data.rows.length === 0) {
					return uni.showToast({
						icon: "error",
						title: "暂无车辆",
						success() {
							uni.navigateTo({
								url: "/pages/traffic/vehicleManagement"
							})
						}
					})
				}
				this.vehicleList = res.data.rows
			})
			// 获取检车地点列表
			http({
				url: "/prod-api/api/traffic/checkCar/place/list",
			}).then((res) => {
				this.checkPlaceList = res.data.rows
			})
		},
		methods: {
			// 单选改变时
			carbindChange: function(item) {
				this.bindCar = item.target.value
			},
			// 修改日期
			dateChange: function(e) {
				this.date = e.detail.value
			},
			// 修改时间
			timeChange: function(e) {
				this.time = e.detail.value
			},
			// 修改检车地点
			checkPlaceChange : function(e) {
				this.checkPlace = this.checkPlaceList[e.detail.value]
				this.checkPlaceShow = false
			},
			// 预约检车地点
			setAppointment : function (e) {
				http({
					url : "/prod-api/api/traffic/checkCar/apply",
					method : "post",
					data : {
						"userId":this.bindCar.userId,
						"carId":this.bindCar.id,
						"aptTime":`${this.date} ${this.time}`,
						"addressId":this.checkPlace.id
					}
				}).then((res) => {
					uni.showToast({
						icon:"success",
						title:"预约成功!"
					})
					uni.navigateTo({
						url:"/pages/traffic/myAppointment"
					})
				})
			}
		}
	}
</script>

<style scoped>
	.mb-4 {
		margin-bottom: 4rem;
	}

	.pd-1 {
		padding: 1rem;
	}

	/* 车辆单选列表 */
	.car-list {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 1rem;

		max-width: 100vw;

	}

	.car-list>*+* {
		margin-top: 1rem;
	}

	.car-list-item {
		display: flex;
		flex: 1;
		min-width: 100%;
		min-height: 0;
		flex-direction: column;
		justify-content: center;
		gap: .5rem;
		padding: 0 1rem .5rem 1rem;
		background-color: aliceblue;
		border-radius: 10px;

	}

	.car-list-item radio {
		margin-top: .5rem;
		align-self: flex-end;
	}

	/* 时间选择器 */
	.date-pick {
		display: flex;
		padding: .5rem;
	}

	.date-pick>*+* {
		margin-left: .5rem;
	}
	
	/* 检车地点选择器 */
	.picker-view {
			width: 100vw;
			height: 200px;
			margin-top: 20rpx;
		}
		.item {
			display: flex;
			height: 50px;
			align-items: center;
			justify-content: center;
			text-align: center;
		}
</style>
